package com.mygdx.game.stage;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.utils.ClickListener;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;
import com.badlogic.gdx.utils.Align;
import com.badlogic.gdx.utils.viewport.Viewport;
import com.mygdx.game.MainGame;
import com.mygdx.game.resource.Res;
import com.mygdx.game.stage.base.BaseStage;

public class WelcomeStage extends BaseStage {

    private Image bg;
    private Image logo;
    private Label label;
    public WelcomeStage(MainGame game, Viewport viewport) {
        super(game, viewport);
        init();
    }

    public void init(){
        bg = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.GAME_BLANK));
        bg.setScale(getWidth()/bg.getWidth(),getHeight()/bg.getHeight());
        addActor(bg);

        logo = new Image(getMainGame().getTextureAtlas().findRegion(Res.Atlas.GAME_LOGO));
        logo.setPosition(getWidth()/2-logo.getWidth()/2,getHeight()/2-logo.getHeight()/2+40);
        addActor(logo);

        Label.LabelStyle style = new Label.LabelStyle();
        style.font = getMainGame().getBitmapFont();
        label = new Label("Click to continue...",style);
        label.setFontScale(0.25f);
        label.setColor(Color.BLACK);
        label.setSize(getWidth(),logo.getPrefHeight());
        label.setAlignment(Align.center);
        label.setPosition(0,60);
        addActor(label);

        new Thread(new Runnable() {
            @Override
            public void run() {
                while (label != null) {
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    if (label==null)
                        break;
                    if (label.isVisible())
                        label.setVisible(false);
                    else
                        label.setVisible(true);
                }
            }
        }).start();
        /*new Thread(new Runnable() {
            @Override
            public void run() {
                flash();
            }
        }).start();
*/
        addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                getMainGame().getGameScreen().setShowHomeStage(true);
                setVisible(false);
                label = null;
            }
        });
    }

    //闪烁动画
    void flash() {
    }

    @Override
    public void dispose(){
        label = null;
    }
}
